#!/bin/bash
source ~/command/common

_project=Server
_oper=0

function getFilePath() {
    file=`find -name $1.java`
    if [ -z "$file" ];then
        echo "请输入正确的类名"
        exit
    fi
    __get_full_path $file
}

function gen_service_code() {
    local  path cls name
    cls="${1%:*}"
    name="${1#*:}"
    if [ "$cls:$name" != "$1" ];then
        echo "格式：class:function"
        exit
    fi

    path=`pwd`
    if [ "${path%${_project}/project}" = "${path}" ];then
        echo "请在工程的project目录运行"
        exit
    fi

    cd $path/src/main/java/com
    cpath=`getFilePath $cls`
    vim -e -s -c ":%s/\r//g" -c ":wq" "$cpath"
    #line=`cat $cpath|grep "public .*$name"|sed 's/.*public\s*//;s/\s*{//'`
    comment=`sed -n '/PostMapping.*'$name'"/{x;p};h' $cpath`
    line=`sed -n '/public .*'$name'(/,/) {/p' $cpath|sed 's/.*public\s*//;s/\s*{//;s/@IfNull[^)]*)\s*//;'| sed '/Long userId/d' | sed ':label;N;s/\n/<--br-->/g;b label'`
    #echo $line
    sname=`echo $cls|sed 's/C\(.\)$/S\1/'`
    spath=`getFilePath $sname`
    vim -e -s -c ":/^}/s#.*#${comment}\r\t${line};\r\r}#" -c ":%s/<--br-->/\r/g" -c ":wq" "$spath"
    siname=`echo $cls|sed 's/C\(.\)$/SI\1/'`
    sipath=`getFilePath $siname`
    vim -e -s -c ":?^}?s#.*#${comment}\r\t@Override\r\tpublic ${line} {\r\t\treturn Result.success();\r\t}\r\r}#" -c ":%s/<--br-->/\r/g" -c ":wq" "$sipath"
}

function copy_file() {
    local  file newFile oldName newName _oldnameFlag=0 _newnameFlag=0 _oldname _oldname1 _newname _newname1
    file="${1%%:*}"
    oldName="${1#*:}"
    newName="${oldName#*:}"
    oldName="${oldName%:*}"

    if [ "$file:$oldName:$newName" != "$1" ];then
        echo "格式：file:oldName:newName"
        exit
    fi

    echo $oldName | grep '^[A-Z]' > /dev/null
    if [ $? -ne 0 ];then
        _oldnameFlag=1;
    fi

    echo $newName | grep '^[A-Z]' > /dev/null
    if [ $? -ne 0 ];then
        _newnameFlag=1;
    fi

    if [ $_oldnameFlag = 1 ];then
        _oldname="$oldName"
        _oldname1=`echo $oldName|sed 's/^[a-z]/\u&/'`
    else
        _oldname1="$oldName"
        _oldname=`echo $oldName|sed 's/^[A-Z]/\l&/'`
    fi

    if [ $_newnameFlag = 1 ];then
        _newname="$newName"
        _newname1=`echo $newName|sed 's/^[a-z]/\u&/'`
    else
        _newname1="$newName"
        _newname=`echo $newName|sed 's/^[A-Z]/\l&/'`
    fi

    newFile=`echo $file|sed 's/'${_oldname}'/'${_newname}'/'|sed 's/'${_oldname1}'/'${_newname1}'/'`

    cp $file $newFile

    vim -e -s -c ":set noignorecase" -c ":%s/${_oldname}/${_newname}/g" -c ":%s/${_oldname1}/${_newname1}/g" -c ":wq" $newFile
}

function copy_class() {
    local  path cls cls1 oldName newName _oldnameFlag=0 _newnameFlag=0 _oldname _oldname1 _newname _newname1
    cls="${1%:*}"
    cls1="${1#*:}"
    if [ "$cls:$cls1" != "$1" ];then
        echo "格式：class:function"
        exit
    fi

    path=`pwd`
    if [ "${path%${_project}/project}" = "${path}" ];then
        echo "请在工程的project目录运行"
        exit
    fi

    oldName=`echo $cls|sed 's/C.$//'`
    newName=`echo $cls1|sed 's/C.$//'`

    echo $oldName | grep '^[A-Z]' > /dev/null
    if [ $? -ne 0 ];then
        _oldnameFlag=1;
    fi

    echo $newName | grep '^[A-Z]' > /dev/null
    if [ $? -ne 0 ];then
        _newnameFlag=1;
    fi

    if [ $_oldnameFlag = 1 ];then
        _oldname="$oldName"
        _oldname1=`echo $oldName|sed 's/^[a-z]/\u&/'`
    else
        _oldname1="$oldName"
        _oldname=`echo $oldName|sed 's/^[A-Z]/\l&/'`
    fi

    if [ $_newnameFlag = 1 ];then
        _newname="$newName"
        _newname1=`echo $newName|sed 's/^[a-z]/\u&/'`
    else
        _newname1="$newName"
        _newname=`echo $newName|sed 's/^[A-Z]/\l&/'`
    fi

    cd $path/src/main/java/com
    cpath=`getFilePath $cls`
    cdir=`dirname $cpath`
    cpath1=${cdir}/${cls1}.java
    if ! [ -f $cpath1 ];then
        if [ -f $cpath ];then
            cp $cpath $cpath1
            vim -e -s -c ":set noignorecase" -c ":%s/${_oldname}/${_newname}/g" -c ":%s/${_oldname1}/${_newname1}/g" -c ":wq" $cpath1
        fi
    fi

    sname=`echo $cls|sed 's/C\(.\)$/S\1/'`
    sname1=`echo $cls1|sed 's/C\(.\)$/S\1/'`
    spath=`getFilePath $sname`
    sdir=`dirname $spath`
    spath1=${sdir}/${sname1}.java
    if ! [ -f $spath1 ];then
        if [ -f $spath ];then
            cp $spath $spath1
            vim -e -s -c ":set noignorecase" -c ":%s/${_oldname}/${_newname}/g" -c ":%s/${_oldname1}/${_newname1}/g" -c ":wq" $spath1
        fi
    fi

    siname=`echo $cls|sed 's/C\(.\)$/SI\1/'`
    siname1=`echo $cls1|sed 's/C\(.\)$/SI\1/'`
    sipath=`getFilePath $siname`
    sidir=`dirname $sipath`
    sipath1=${sidir}/${siname1}.java
    if ! [ -f $sipath1 ];then
        if [ -f $sipath ];then
            cp $sipath $sipath1
            vim -e -s -c ":set noignorecase" -c ":%s/${_oldname}/${_newname}/g" -c ":%s/${_oldname1}/${_newname1}/g" -c ":wq" $sipath1
        fi
    fi

    ename=`echo $cls|sed 's/C.$/E/'`
    ename1=`echo $cls1|sed 's/C.$/E/'`
    epath=`getFilePath $ename`
    edir=`dirname $epath`
    epath1=${edir}/${ename1}.java
    if ! [ -f $epath1 ];then
        if [ -f $epath ];then
            cp $epath $epath1
            vim -e -s -c ":set noignorecase" -c ":%s/${_oldname}/${_newname}/g" -c ":%s/${_oldname1}/${_newname1}/g" -c ":wq" $epath1
        fi
    fi

    ername=`echo $cls|sed 's/C.$/ER/'`
    ername1=`echo $cls1|sed 's/C.$/ER/'`
    erpath=`getFilePath $ername`
    erdir=`dirname $erpath`
    erpath1=${erdir}/${ername1}.java

    if ! [ -f $erpath1 ];then
        if [ -f $erpath ];then
            cp $erpath $erpath1
            vim -e -s -c ":set noignorecase" -c ":%s/${_oldname}/${_newname}/g" -c ":%s/${_oldname1}/${_newname1}/g" -c ":wq" $erpath1
        fi
    fi
}

function gen_setter_getter() {
    path=`pwd`
    if [ "${path%${_project}/project}" = "${path}" ];then
        echo "请在工程的project目录运行"
        exit
    fi

    files=(`ls ${path}/src/main/java/com/whxx/sp/entity/*.java`)
    for file in "${files[@]}";do
        vim -e -s -c ":%s/private\s\+\(\w\+\)\s\+\(\w\+\)\([^;]*\);.*/\=\"private \".submatch(1).\" \".submatch(2).submatch(3).\";public \".submatch(1).\" get\".toupper(strpart(submatch(2), 0, 1)).strpart(submatch(2), 1).\"(){ return this.\".submatch(2).\"; }public void set\".toupper(strpart(submatch(2), 0, 1)).strpart(submatch(2), 1).\"(\".submatch(1).\" \".submatch(2).\"){ if(null != \".submatch(2).\"){ this.\".submatch(2).\" = \".submatch(2).\"; } }\"/" -c ":wq" "$file"
    done
}

function genrate_param() {
    if __mac ;then
        pbpaste|sed 's#\s*//.*$##'|sed 's/.* //'|pbcopy
    else
        cat /dev/clipboard|sed 's#\s*//.*$##'|sed 's/.* //' > /dev/clipboard
    fi
}

function genrate_param_from_entity() {
    if __mac ;then
        pbpaste|sed '/@Column/d'|sed 's/private //'|sed -n 'h;$!{n;G};p'|sed -n '{N;s/\n/\t/p}'|sed 's/\s\+/ /g;s/;/,/;s/\s*=\s*[^,]*,/,/;$s/,//'|pbcopy
    else
        cat /dev/clipboard|sed '/@Column/d'|sed 's/private //'|sed -n 'h;$!{n;G};p'|sed -n '{N;s/\n/\t/p}'|sed 's/\s\+/ /g;s/;/,/;s/\s*=\s*[^,]*,/,/;$s/,//' > /dev/clipboard
    fi
}

function clear_ifnull() {
    if __mac ;then
        pbpaste|sed 's/@IfNull[^)]*)\s*//'|pbcopy
    else
        cat /dev/clipboard|sed 's/@IfNull[^)]*)\s*//' > /dev/clipboard
    fi
}

function genrate_param_for_js() {
    if __mac ;then
        pbpaste|sed "s#.* \(\w\+\)[,]*\s*\(//.*\)#\1: '', \2#"|pbcopy
    else
        cat /dev/clipboard|sed "s#.* \(\w\+\)[,]*\s*\(//.*\)#\1: '', \2#" > /dev/clipboard
    fi
}

function show_help()
{
    local -a list
    list="help"
    list=("${list[@]}" "Usage :xidea [OPTIONS]")
    list=("${list[@]}" "  -s [class:function]: generate service code")
    list=("${list[@]}" "  -f [file:oldName:newName]: copy file")
    list=("${list[@]}" "  -c [oldClass:newClass]: copy Controller Service and ServerImpl")
    list=("${list[@]}" "  -e: genrate param from entity in clipboard")
    list=("${list[@]}" "  -p: genrate param without comment in clipboard")
    list=("${list[@]}" "  -j: genrate param for js")
    list=("${list[@]}" "  -i: delete @IfNull in clipboard")
    list=("${list[@]}" "  -h: show help")
    __msgbox "${list[@]}"
}

function main()
{
    local oper_right param
    oper_right=0

    if [ "$1" = "--help" ];then
        show_help full
        exit
    fi

    while getopts :s:f:c:peijh opt;do
        oper_right=1
        case $opt in
            s)_oper=0;param="$OPTARG";;
            f)_oper=1;param="$OPTARG";;
            c)_oper=2;param="$OPTARG";;
            p)_oper=3;;
            i)_oper=4;;
            j)_oper=5;;
            e)_oper=6;;
            *)show_help;exit;;
        esac
    done

    case $_oper in
        0)gen_service_code $param;;
        1)copy_file $param;;
        2)copy_class $param;;
        3)genrate_param;;
        4)clear_ifnull;;
        5)genrate_param_for_js;;
        6)genrate_param_from_entity;;
    esac
}

main "$@"
